<?php
namespace app\app\controller\v1;
use app\app\Appbase;
use think\facade\Session;
use think\facade\Cache;
use base\service\MsgApi;

class Shop extends Appbase 
{
	
	/**
	* 获取分类
	* /app/v1/shop/getCates
	*/
	public function getCates(){
		$model = model('ShopCate');
		$model = $model->where("is_del",0);
		$model = $model->field("id,name");
		$model = $model->order("sort_no");
		$cates = $model->select()->toArray();
		array_unshift($cates,["name"=>"全部","value"=>0]);    
		return $this->ReSucess($cates);	
	}
	
	/**
	* 获取商家申请等级
	* /app/v1/shop/getApplyLevelDesc
	* 需要登录 
	* @is_need_login true
	*/
	public function getApplyLevel(){
		
		$result = model('UserLevel')
			->with([
				'goodsInfo'=>function($query){
					$query->field('id,name as goods_name');
				},
				'userLevel'
			])
			->where("level",'in',"4,5,6,7,8,9")
			->field('name,level,goods_id,buy_number,specified_level,level_number')
			->select();
			
		foreach( $result as $key=>$row){
			if($row['level'] < 7 ){
				$complete = model('order')->alias('a')
					->leftJoin("order_goods b",'a.id = b.order_id')
					->where("a.uid",request() ->userId)
					->where("a.is_pay",1)
					->where("a.status",">=",0)
					->where("b.goods_id",$row['goods_id'])
					->sum('b.num');	
				$row['goods_name'] = $row['goods_info']['goods_name'] ?? '';
				$row['desc'] = '购买会员商品 '.$row['goods_name'].' 数量达到'.($row['buy_number'] ?: 0);
				$row['desc2'] = '完成情况 '.$complete.' /'.($row['buy_number'] ?: 0);
			}else{
				$complete = model('user')->alias('a')
					->leftJoin("user_vip b",'a.id = b.uid')
					->where("a.spread_uid",request() ->userId)
					->where("b.level",$row['specified_level'])
					->count();	
				$name= $row['user_level']['name'] ?? '';
				$row['desc'] = '推荐会员等级 '.$name.' 数量达到'.($row['level_number'] ?: 0);
				$row['desc2'] = '完成情况 '.$complete.' /'.($row['level_number'] ?: 0);
			}
			
			unset($row['goods_info']);
			unset($row['user_level']);
		}
			
		return $this->ReSucess($result);	
	}

	
	/**
	* 商家申请
	* /app/v1/shop/apply
	* 需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function apply(){
		$request = request();
		$param = $request->post();
		$param['uid'] = $request ->userId;
		try {
			model('ShopApply')->addApply($param);
			return $this->ReSucess('','申请成功，等待审核！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 我的申请记录
	* /app/v1/shop/myApply
	* 需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function myApply(){
		$request = request();
		try {
			$res = model('ShopApply')
				->with("levelInfo")
				->where('uid',request() ->userId)
				->field('id,status,status_desc,level,add_time')
				->field("
					if(status = -1,'已驳回', if(status = 1,'已通过','审核中') ) as status_name
				")
				->order('id','desc')
				->select();
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取商家信息
	* /app/v1/shop/getShopInfo
	* 需要登录 
	* @is_need_login true
	*/
	public function getShopInfo($proj_id=''){
		try {
			$shop_id = request()->shopId;
			if($proj_id){
				$shop_id = model('user_yg_proj')->where('id',$proj_id)->value('shop_id');
			}
			$result = model('Shop')->getShopInfo($shop_id);
			//$result && model('GoodsHistory') ->addHistory(request()->userId,$shop_id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取员工信息
	* /app/v1/shop/getShopInfo
	* 需要登录 
	* @is_need_login true
	*/
	public function getStaffInfo($staff_id){
		try {
			$result = model('ShopStaff')->get($staff_id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	


	/**
	* 最近预览厂家列表
	* /app/v1/shop/latelyPreview
	* 需要登录 
	* @is_need_login true
	*/
	public function latelyPreview(){
		try {
			$dbShop = model('Shop');
			$result = $dbShop->latelyPreview(request() ->userId);
			if(!$result){
				return $this->ReError(  $dbShop->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 收藏或取消收藏
	* /app/v1/shop/collect
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function collect($shop_id){
		
		$uid = request() ->userId;
		$param['uid'] =$uid;
		$param['goods_id'] = 0;
		$param['type'] = 'collect';
		$param['shop_id'] = $shop_id;
		
		if(!$param['shop_id']){
			return $this->ReError("找不到该商家！");
		}
		
		try {
			$db = model('UserStars');
			$is_collect = $db->where('uid',$uid)->where('type','collect')
				->where('shop_id',$shop_id)
				->find();
				
			if( !$is_collect  ){
				$result = $db->add($param);
			}else{
				$result = $db->delShopCollect($uid,$shop_id);
			}
			
			if($result === false){
				return $this->ReError( $db->errorInfo );
			}
			return $this->ReSucess(null,  $is_collect ? '已取消收藏！' : '收藏成功！');	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取员工列表
	* /app/v1/shop/getStaffList
	* 需要登录 
	* @is_need_login true
	*/
	public function getStaffList(){
		try {
			$result = model('ShopStaff')->getList(request()->shopId);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取员工列表
	* /app/v1/shop/complaint
	* 需要登录 
	* @is_need_login true
	*/
	public function complaint($staff_id,$descs){
		try {
			$uid = request()->userInfo['id'] ?: '-1';
			$shop_id = request()->shopId ?: '-1';
			model('ShopComplaint')->save([
				'uid'=>$uid,
				'shop_id'=>$shop_id,
				'staff_id'=>$staff_id,
				'descs'=>$descs,
			]);
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
}



